#include <QCoreApplication>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPushButton>

#include <pluginui/widget/StandardMainDialog.h>
#include <pluginui/extension/StandardMainDialogExtension.h>
#include <plugin/util/Framework.h>
#include <pluginui/widget/StandardAboutDialog.h>
#include <pluginui/widget/StandardPluginListDialog.h>

namespace antu
{
namespace plugin
{
namespace ui
{

PluginVersion StandardMainDialog::version() const
{
    return m_oVersion;
}

void StandardMainDialog::setVersion(const PluginVersion &version)
{
    m_oVersion = version;
}

QString StandardMainDialog::serial() const
{
    return m_sSerial;
}

void StandardMainDialog::setSerial(const QString &serial)
{
    m_sSerial = serial;
}

QPixmap StandardMainDialog::aboutImage() const
{
    return m_oAboutImage;
}

void StandardMainDialog::setAboutImage(const QPixmap &aboutImage)
{
    m_oAboutImage = aboutImage;
}

StandardMainDialog::StandardMainDialog(PluginContext* pContext, AbstractSecurityContextHandler* pHandler, QWidget* parent)
    : QWidget(parent),
      m_pContext(pContext)
{
    Q_ASSERT(m_pContext != nullptr);

    QPushButton* pPluginListButton = new QPushButton(tr("Plugins..."), this);
    QPushButton* pAboutButton = new QPushButton(tr("About..."), this);
    QPushButton* pExitButton = new QPushButton(tr("Exit"), this);

    QHBoxLayout* pMainLayout = new QHBoxLayout;
    this->setLayout(pMainLayout);

    QWidget* pContainerWidget = new QWidget;
    pContainerWidget->setContentsMargins(0, 0, 0, 0);
    pMainLayout->addWidget(pContainerWidget, 1);

    QVBoxLayout* pButtonLayout = new QVBoxLayout;
    pMainLayout->addLayout(pButtonLayout);
    pButtonLayout->addWidget(pPluginListButton);
    pButtonLayout->addWidget(pAboutButton);
    pButtonLayout->addWidget(pExitButton);
    pButtonLayout->addStretch(1);

    connect(pPluginListButton, SIGNAL(clicked()), this, SLOT(pluginListClicked()));
    connect(pAboutButton, SIGNAL(clicked()), this, SLOT(aboutClicked()));
    connect(pExitButton, SIGNAL(clicked()), qApp, SLOT(quit()));

    Framework::Instance.setup(this->m_pContext, pHandler);
    Framework::Instance.loadTranslationFile(this->m_oTranslator, "pluginui");

    AbstractExtension* pExtension = new StandardMainDialogExtension(pContainerWidget);
    this->m_pActivator = new GuiPluginActivator(pExtension);

    Framework::Instance.logger()->debug(__FILE__, __LINE__, "StandardMainDialog", "ctor", "Starting activator");
    this->m_pActivator->init(this);
    this->m_pActivator->start();
    Framework::Instance.logger()->debug(__FILE__, __LINE__, "StandardMainDialog", "ctor", "Started activator");

	this->m_pActivator->finishStartup();

	this->m_sSerial = "0000 0000 0000 0000 0000";
	this->m_oVersion = PluginVersion("1.0.0.0");
}

StandardMainDialog::~StandardMainDialog()
{
    Framework::Instance.logger()->debug(__FILE__, __LINE__, "StandardMainDialog", "dtor", "Stopping activator");
    this->m_pActivator->stop();
    delete this->m_pActivator;
    Framework::Instance.logger()->debug(__FILE__, __LINE__, "StandardMainDialog", "dtor", "Stopped activator");
}

void StandardMainDialog::aboutClicked()
{
    StandardAboutDialog dlg(this->windowTitle(), this->windowTitle(),
                            this->version().toString(), this->serial(),
                            this->aboutImage(), this);
    dlg.exec();
}

void StandardMainDialog::pluginListClicked()
{
    StandardPluginListDialog dlg(this);
    dlg.exec();
}

void StandardMainDialog::closeEvent(QCloseEvent *pEvent)
{
    CloseSignal signal;
    Framework::Instance.stop(&signal);

    if (signal.canClose())
        QWidget::closeEvent(pEvent);
}

}
}
}
